package com.exam.service.impl;

import com.exam.mapper.PositionMapper;
import com.exam.mapper.StudentMapper;
import com.exam.pojo.entity.Student;
import com.exam.pojo.mode.StudentVo;
import com.exam.service.PositionService;
import com.exam.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private PositionService positionService;

    /**
     * 查询所有学生列表
     *
     * @return
     */
    @Override
    public List<StudentVo> findAll() {
        return studentMapper.findAll();
    }


    /**
     * 添加人员
     * @param student
     */
    @Override
    public void addStudent(Student student) {
        //根据 positionId 找到 departmentId
        Integer positionId = student.getPositionId();
        Integer departmentId = positionService.findDepartmentIdByPositionId(positionId);

        //添加字段
        student.setDepartmentId(departmentId);
        student.setRoleId(5);

        System.out.println(student);
        //插入数据库
        int insert = studentMapper.insert(student);
        System.out.println(insert);
    }

    /**
     * 删除学生信息
     * @param studentId
     */
    @Override
    public void delStudent(Integer studentId) {
        Student student = studentMapper.selectById(studentId);
        student.setDelFlag(0);
        studentMapper.updateById(student);
//        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Student::getId,studentId);
//        studentMapper.update(student,queryWrapper);
    }

    /**
     * 更新人员信息
     * @param student
     */
    @Override
    public void editStudent(Student student) {
        //查询是否存在
        Student s = studentMapper.selectById(student.getId());
        //存在更新
        if(s != null){
            studentMapper.updateById(student);
        }else{
            //不存在插入
            this.addStudent(student);
        }
    }

    /**
     * 模糊查询
     * @param searchStudentVo
     * @return
     */
    @Override
    public List<StudentVo> findStudentByDidOrPidOrName(SearchStudentParam searchStudentVo) {
        //根据 positionId 找到 departmentId
        Integer positionId = searchStudentVo.getPositionId();
        Integer departmentId = searchStudentVo.getDepartmentId();
        if(departmentId == null && positionId != null){
            departmentId = positionService.findDepartmentIdByPositionId(positionId);
            searchStudentVo.setDepartmentId(departmentId);
        }
        return studentMapper.findStudentByDidOrPidOrName(searchStudentVo);
    }

    /**
     * 分页查询所有学生列表
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public List<StudentVo> listStudent(Integer currentPage,Integer size) {
        return studentMapper.findAllPage(currentPage, size);
    }

    /**
     * 批量删除
     * @param studentIdList
     */
    @Override
    public void delAllStudent(List<Integer> studentIdList) {
        for (Integer studentId : studentIdList) {
            this.delStudent(studentId);
        }
    }

}
